package com.guli.edu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.constants.ResultCodeEnum;
import com.guli.common.exception.GuliException;
import com.guli.common.vo.R;
import com.guli.edu.entity.Teacher;
import com.guli.edu.query.TeacherQuery;
import com.guli.edu.service.TeacherService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Frank
 * @since 2019-04-15
 */
@Api(tags = "讲师管理", description = "后台讲师管理")
//跨域
@CrossOrigin
@RestController
@RequestMapping("admin/edu/teacher")
public class TeacherAdminController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 插入讲师记录
     *
     * @param teacher 讲师对象
     * @return 全局统一结果R对象
     */
    @PostMapping
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher
    ) {
        final boolean bsave = teacherService.save(teacher);

        return bsave == true ? R.ok() : R.error();
    }

    /**
     * 逻辑删除讲师记录
     *
     * @param id 要删除的讲师id
     * @return boolean true-success false-fail
     */
    @ApiOperation(value = "根据id删除讲师", notes = "url传入单个id删除")
    @DeleteMapping("{id}")
    public R deleteById(
            @ApiParam(value = "讲师id", name = "id", required = true)
            @PathVariable String id) {
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 修改讲师记录
     */

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {

        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }

    /**
     * 查询讲师
     *
     * @param id 讲师id
     * @return 全局统一结果R对象
     */

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    /**
     * 查询所有讲师列表
     *
     * @return 全局统一结果R对象
     */
    @ApiOperation(value = "所有讲师列表", notes = "查询所有讲师列表")
    @GetMapping
    public R list() {
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("item", list);

    }

    /**
     * 模糊查询分页讲师列表
     *
     * @param page         页码
     * @param limit        每页记录数
     * @param teacherQuery 查询对象
     * @return 全局统一结果R对象
     */

    @ApiOperation(value = "模糊查询分页讲师列表")
    @GetMapping(value = "/{page}/{limit}")
    public R likeQueryPageList(
            @ApiParam(name = "page", value = "页码", required = true)
            @PathVariable("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象")
                    TeacherQuery teacherQuery) {

        //分页参数检查
        //throw new GuliException(21003, "参数不正确1");
        if (page <= 0 || limit <= 0) {
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }

        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, teacherQuery);
        List<Teacher> teacherList = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("rows", teacherList).data("total", total);

    }



}
